package cn.six1943.deepchat.module.ai.controller.admin.provider;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.six1943.deepchat.module.ai.controller.admin.provider.vo.*;
import cn.six1943.deepchat.module.ai.dal.dataobject.provider.ProviderDO;
import cn.six1943.deepchat.module.ai.service.provider.ProviderService;

@Tag(name = "管理后台 - AI提供商")
@RestController
@RequestMapping("/ai/provider")
@Validated
public class ProviderController {

    @Resource
    private ProviderService providerService;

    @PostMapping("/create")
    @Operation(summary = "创建AI提供商")
    @PreAuthorize("@ss.hasPermission('ai:provider:create')")
    public CommonResult<Long> createProvider(@Valid @RequestBody ProviderSaveReqVO createReqVO) {
        return success(providerService.createProvider(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新AI提供商")
    @PreAuthorize("@ss.hasPermission('ai:provider:update')")
    public CommonResult<Boolean> updateProvider(@Valid @RequestBody ProviderSaveReqVO updateReqVO) {
        providerService.updateProvider(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除AI提供商")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:provider:delete')")
    public CommonResult<Boolean> deleteProvider(@RequestParam("id") Long id) {
        providerService.deleteProvider(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得AI提供商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:provider:query')")
    public CommonResult<ProviderRespVO> getProvider(@RequestParam("id") Long id) {
        ProviderDO provider = providerService.getProvider(id);
        return success(BeanUtils.toBean(provider, ProviderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得AI提供商分页")
    @PreAuthorize("@ss.hasPermission('ai:provider:query')")
    public CommonResult<PageResult<ProviderRespVO>> getProviderPage(@Valid ProviderPageReqVO pageReqVO) {
        PageResult<ProviderDO> pageResult = providerService.getProviderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProviderRespVO.class));
    }

}